<template>
  <div class="product-card" @click="goToDetail">
    <!-- 商品图片 -->
    <div class="product-image">
      <img :src="getProductImage()" :alt="product.name" />
      <div class="product-overlay">
        <a-button  @click.stop="addToCart">
          <ShoppingCartOutlined />
        </a-button>
        <a-button @click.stop="toggleFavorite">
          <HeartOutlined v-if="!isFavorite" />
          <HeartFilled v-else style="color: #ff4d4f" />
        </a-button>
      </div>
    </div>
    
    <!-- 商品信息 -->
    <div class="product-info">
      <h3 class="product-name" :title="product.name">{{ product.name }}</h3>
      
      <!-- 价格信息 -->
      <div class="product-price">
        <span class="current-price">¥{{ product.price }}</span>
        <span v-if="product.originalPrice > product.price" class="original-price">
          ¥{{ product.originalPrice }}
        </span>
      </div>
      
      <!-- 评分和评价 -->
      <div class="product-rating">
        <a-rate :value="product.rating" disabled allow-half />
        <span class="rating-text">{{ product.rating }}</span>
        <span class="review-count">({{ product.reviews }}条评价)</span>
      </div>
      
      <!-- 商品描述 -->
      <p class="product-description">{{ product.description }}</p>
      
      <!-- 库存信息 -->
      <div class="product-stock">
        <span v-if="product.stock > 0" class="in-stock">库存: {{ product.stock }}件</span>
        <span v-else class="out-of-stock">暂时缺货</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { ShoppingCartOutlined, HeartOutlined, HeartFilled } from '@ant-design/icons-vue'

// 定义props
const props = defineProps({
  product: {
    type: Object,
    required: true
  }
})

// 定义emits
const emit = defineEmits(['add-to-cart'])

// 使用Vuex store和Vue Router
const store = useStore()
const router = useRouter()

// 计算属性
const isFavorite = computed(() => {
  // 这里应该从store中获取收藏状态
  return false
})

// 方法
const getProductImage = () => {
  // 如果商品有images数组，使用第一张图片
  if (props.product.images && props.product.images.length > 0) {
    return props.product.images[0]
  }
  // 如果商品有image字段，使用image
  if (props.product.image) {
    return props.product.image
  }
  // 默认图片
  return 'https://picsum.photos/300/300?random=999'
}

const goToDetail = () => {
  router.push(`/product/${props.product.id}`)
}

const addToCart = () => {
  if (props.product.stock <= 0) {
    message.warning('该商品暂时缺货')
    return
  }
  
  store.dispatch('cart/addToCart', {
    ...props.product,
    quantity: 1
  })
  
  message.success('已添加到购物车')
  emit('add-to-cart', props.product)
}

const toggleFavorite = () => {
  // 这里应该调用收藏/取消收藏的API
  message.success(isFavorite.value ? '已取消收藏' : '已添加到收藏')
}
</script>

<style scoped>
.product-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.product-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

.product-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  min-width: 200px;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.product-card:hover .product-image img {
  transform: scale(1.05);
}

.product-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  /* flex-direction: column; */
  justify-content: center;
  align-items: center;
  gap: 10px;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.product-card:hover .product-overlay {
  opacity: 1;
}

.product-info {
  padding: 15px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.product-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin: 0 0 10px 0;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.product-price {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.current-price {
  font-size: 18px;
  font-weight: bold;
  color: #ff4d4f;
}

.original-price {
  font-size: 14px;
  color: #999;
  text-decoration: line-through;
}

.product-rating {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-bottom: 10px;
}

.rating-text {
  font-size: 14px;
  color: #666;
  font-weight: 500;
}

.review-count {
  font-size: 12px;
  color: #999;
}

.product-description {
  font-size: 14px;
  color: #666;
  line-height: 1.4;
  margin: 0 0 10px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  flex: 1;
}

.product-stock {
  margin-top: auto;
}

.in-stock {
  font-size: 12px;
  color: #52c41a;
}

.out-of-stock {
  font-size: 12px;
  color: #ff4d4f;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .product-image {
    height: 150px;
  }
  
  .product-info {
    padding: 12px;
  }
  
  .product-name {
    font-size: 14px;
  }
  
  .current-price {
    font-size: 16px;
  }
  
  .product-overlay {
    flex-direction: row;
    gap: 5px;
  }
  
  .product-overlay .ant-btn {
    padding: 4px 8px;
    font-size: 12px;
  }
}
</style> 